﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;

namespace Taxes.Taxeses
{
    public class Taxeses
    {
        public String Code { get; set; }
        public String Name { get; set; }
        public Dictionary<String, float> Taxe { get; set; }
        public static List<Taxeses> Analises(String path)
        {
            List<Taxeses> taxeses = new List<Taxeses>();
            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                var workBook = new HSSFWorkbook(stream);
                var enumerator = workBook.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    var sheet = (HSSFSheet)enumerator.Current;
                    if (sheet == null) continue;
                    var rowEnumerator = sheet.GetEnumerator();
                    bool needNew = false;
                    Taxeses taxes = new Taxeses();
                    taxes.Taxe = new Dictionary<string, float>();
                    ;
                    while (rowEnumerator.MoveNext())
                    {
                        var row = (HSSFRow)rowEnumerator.Current;
                        if (row == null)
                            break;
                        if (row.RowNum < 2)
                        {
                            continue;
                        }
                        var cellEnumerator = row.GetEnumerator();

                        if (needNew)
                        {
                            taxes = new Taxeses();
                            taxes.Taxe = new Dictionary<string, float>();
                        }


                        String key = String.Empty;
                        while (cellEnumerator.MoveNext())
                        {
                            var cell = cellEnumerator.Current as HSSFCell;
                            if (cell == null) continue;

                            if (cell.ColumnIndex == 0 && cell.StringCellValue != "")
                            {
                                taxes.Code = cell.StringCellValue;
                                needNew = false;
                            }
                            else if (cell.ColumnIndex == 1)
                            {
                                needNew = false;
                                if (cell.StringCellValue.Trim() == "小计")
                                {
                                    needNew = true;
                                    break;
                                }
                                if (cell.StringCellValue.Trim() == "合计")
                                    break;
                                taxes.Name = cell.StringCellValue;
                            }
                            else if (cell.ColumnIndex == 2)
                            {
                                taxes.Taxe.Add(cell.StringCellValue, 0);
                                key = cell.StringCellValue;
                            }
                            else if (cell.ColumnIndex == 4)
                            {
                                switch (cell.CellType)
                                {
                                    case CellType.NUMERIC:
                                        taxes.Taxe[key] = (float)cell.NumericCellValue;
                                        break;
                                    case CellType.STRING:
                                        float value;

                                        if (float.TryParse(cell.StringCellValue, out value))
                                        {
                                            taxes.Taxe[key] = value;
                                        }
                                        break;
                                }

                            }

                        }
                        if (needNew)
                            taxeses.Add(taxes);
                    }
                }
            }
            return taxeses;
        }
    }

}